package com.imti.retrofitweatherdemo;

import android.content.Context;
import android.os.Environment;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

/**
 * Glide全局配置
 */
@GlideModule
public class MyAppGlideModule extends AppGlideModule
{

    //外部路径
    private String sdRootPath = Environment.getExternalStorageDirectory().getPath();

    private String appRootPath = null;

    private String getStorageDirectory()
    {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ?
                   sdRootPath : appRootPath;
    }

    /**
     * Glide全局配置入口
     * @param context
     * @param builder Glide全局配置构造类
     */
    @Override
    public void applyOptions(@NonNull   Context context,
        @NonNull   GlideBuilder builder)
    {
        builder
            //20M内存图片缓存
            .setMemoryCache(new LruResourceCache(20 * 1024 * 1024))
            //100M硬盘缓存
            //默认路径
//            .setDiskCache(new InternalCacheDiskCacheFactory(context, 100 * 1024 * 1024))
            //指定路径
            .setDiskCache(
                new DiskLruCacheFactory(getStorageDirectory() + "/image_cache",
                    100 * 1024 * 1024))
            .addGlobalRequestListener(new RequestListener()
            {
                @Override
                public boolean onLoadFailed(
                    @Nullable  GlideException e, Object model,
                    Target target, boolean isFirstResource)
                {
                    return false;
                }
                @Override
                public boolean onResourceReady(Object resource, Object model, Target target,
                    DataSource dataSource, boolean isFirstResource)
                {
                    return false;
                }
            });
        super.applyOptions(context, builder);
    }

    public static void loadRoundImage(Context context, String url, ImageView image)
    {
        Glide.with(context)
            //以图片形式显示，若以GIF显示动图，.asGif()
            .asBitmap()
            //设置加载图片尺寸，矩形尺寸
            .override(24, 24)
            //正方形尺寸
//            .override(24)
            //预加载图片，正在加载...
            .placeholder(R.mipmap.ic_launcher)
            //加载失败图
            .error(R.mipmap.ic_launcher)
            .load(url)
            //裁剪方式，圆形裁剪
            .circleCrop()
            //裁剪方式，中心放大裁剪，等同于android:scaleType="center"
            .centerCrop()
            .into(image);
    }
}
